﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using iOSRemoteConnector;
using System.IO;
using System.Collections.ObjectModel;

namespace MediaSurfaceV2
{
    /// <summary>
    /// Interaction logic for iPhoneVisualization.xaml
    /// </summary>
    public partial class iPhoneVisualization : TagVisualization
    {
        private ObservableCollection<Image> mediaListObservable = new ObservableCollection<Image>();

        public iPhoneVisualization()
        {
            InitializeComponent();

            //Chris's iPhone
            iOSDevice dev = Devices.GetDevice("a0377f45a9caa08e42a902928a6f9adb1361604e");
        
            //Teddy's iPhone
            //iOSDevice dev = Devices.GetDevice("db3d69d0b58fd067719bc5fbfe188372b1afd23e");
            
            //Zabed's iPhone
            //iOSDevice dev = Devices.GetDevice("793da33aa0c9b3f3133db2c183a84afd3a1f239e");

            iOSDevice.OnImageReceivedDelegate myDelegate = delegate(Image image)
            {
                  setImage(image);
            };

            dev.onImageReceivedDelegate = myDelegate;
            iPhoneDataBox.ItemsSource = mediaListObservable;
        }

        private void iPhoneVisualization_Loaded(object sender, RoutedEventArgs e)
        {
            //TODO: customize iPhoneVisualization's UI based on this.VisualizedTag here
        }

        public long? TagValue
        {
            get { return (long?)GetValue(TagValueProperty); }
            set { SetValue(TagValueProperty, value); }
        }
        public static readonly DependencyProperty TagValueProperty =
            DependencyProperty.Register("TagValue", typeof(long?), typeof(iPhoneVisualization));

        protected override void OnGotTag(RoutedEventArgs e)
        {
            if (VisualizedTag != null)
            {
                TagValue = (VisualizedTag.Type == TagType.Byte) ?
                    VisualizedTag.Byte.Value :
                    VisualizedTag.Identity.Value;
            }
            base.OnGotTag(e);
        }


        //private void contentImage1_ContactDown(object sender, ContactEventArgs e)
        //{
        //    Console.Out.WriteLine("touch me baby one more time");
        //    SurfaceBridge sb = new SurfaceBridge();

        //    //sb.RequestFullMovie(1, contentImage1.Name);
        //}

        public void setImage(Image img)
        {
            //Image dd = new Image();
            //dd.Source = img;

            mediaListObservable.Add(img);
        }

        public ObservableCollection<Image> MediaListObservable
        {
            get
            {
                return mediaListObservable;
            }
        }

        private void iPhoneDataBox_PreviewContactDown(object sender, ContactEventArgs e)
        {
            FrameworkElement findSource = e.OriginalSource as FrameworkElement;
            SurfaceListBoxItem draggedElement = null;

            // Find the touched SurfaceListBoxItem object.
            while (draggedElement == null && findSource != null)
            {
                if ((draggedElement = findSource as SurfaceListBoxItem) == null)
                {
                    findSource = VisualTreeHelper.GetParent(findSource) as FrameworkElement;
                }
            }

            if (draggedElement == null)
            {
                return;
            }

            // Create the cursor visual.
            ContentControl cursorVisual = new ContentControl()
            {
                Content = draggedElement.DataContext,
                Style = FindResource("CursorStyle") as Style
            };

            // Add a handler. This will enable the application to change the visual cues.
            SurfaceDragDrop.AddTargetChangedHandler(cursorVisual, OnTargetChanged);

            // Create a list of input devices. Add the contacts that
            // are currently captured within the dragged element and
            // the current contact (if it isn't already in the list).
            List<InputDevice> devices = new List<InputDevice>();
            devices.Add(e.Contact);
            foreach (Contact contact in draggedElement.ContactsCapturedWithin)
            {
                if (contact != e.Contact)
                {
                    devices.Add(contact);
                }
            }

            // Get the drag source object
            ItemsControl dragSource = ItemsControl.ItemsControlFromItemContainer(draggedElement);

            bool startDragOkay =
                SurfaceDragDrop.BeginDragDrop(
                  dragSource,                 // The SurfaceListBox object that the cursor is dragged out from.
                  draggedElement,             // The SurfaceListBoxItem object that is dragged from the drag source.
                  cursorVisual,               // The visual element of the cursor.
                  draggedElement.DataContext, // The data associated with the cursor.
                  devices,                    // The input devices that start dragging the cursor.
                  DragDropEffects.Move);      // The allowed drag-and-drop effects of the operation.

            // If the drag began successfully, set e.Handled to true. 
            // Otherwise SurfaceListBoxItem captures the contact 
            // and causes the drag operation to fail.
            e.Handled = startDragOkay;
        }

        private void OnTargetChanged(object sender, TargetChangedEventArgs e)
        {
            if (e.Cursor.CurrentTarget != null)
            {
                Image data = e.Cursor.Data as Image;
               
            }
            else
            {
                e.Cursor.Visual.Tag = null;
            }
        }
    }
}
